/**
 * Created by IntelliJ IDEA.
 * User: ryan
 * Date: Sep 14, 2010
 * Time: 5:35:08 PM
 * The purpose of this is to convert back and forth between a line of text and json objects that represent
 * Topics, Actionables, Information
 */

var lineShark = lineShark || {};

lineShark.eatLine = function(text, context) {
    var result = {};
    text = $.trim(text);
    var remainder = lineShark.setType(text, result);
    var splitRemainder = lineShark.eatMainText(remainder, result);
    lineShark.eatScraps(splitRemainder, result, context);
    lineShark.addContext(result, context);
    return result;
}

lineShark.setType = function(text, result) {
    if (text && text[0] == '[' && text[1] == ']') {
        result.type = 'actionable';
        return text.substring(2);
    }
    if (text && text[0] == '?') {
        result.type = 'actionable';
        result.subtype = 'question';
        return text.substring(1);
    }
    if (text && text[0] == '#') {
        result.type = 'topicChange';
        var topic =  text.substring(1);
        result.topic = topic;
        return null;

    }

    // default to reference
    result.type = 'reference';
    return text;
}


lineShark.eatMainText = function(text, result) {
    if (!text) return;
    text = $.trim(text); //kill white space
    var split = text.split(/(#|\^|@|!)/);
    if (split && split.length > 1) {
        result.text = $.trim(split[0]);
        var remainder = $.grep(split, function(n, i) {
            return (i != 0);
        });
        return remainder;
    } else {
        result.text = text;
        return null;
    }
}

lineShark.eatScraps = function(splitRemainder, result, context) {
    var currentStack = null;
    if (!splitRemainder || splitRemainder.length == 0) return;
    $.each(splitRemainder, function(i, element) {
        if (element == '#') currentStack = 'topic';
        else if (element == '^') currentStack = 'date';
        else if (element == '@') currentStack = 'context';
        else if (element == '!') currentStack = 'delegate';
        else {
            if (currentStack) {
                if (currentStack == 'date') {
                    result[currentStack] = Date.parse(element).getTime();
                }
                else if(currentStack == 'delegate' && element == 'me' && context && context.me ) {
                    result[currentStack] = context.me;
                }
                else {
                    result[currentStack] = $.trim(element);
                }



                currentStack = null;
            }
        }
    });
}

lineShark.addContext = function(result, context) {
    if (!context) return;
    if (context.conversation) {
        result.conversation = context.conversation;
    }
    if (context.timestamp) {
        result.timestamp = context.timestamp;
    }
    if (context.topicStack && context.topicStack.length > 0) {
        var topicStack = [];
        $.each(context.topicStack, function(i, elem) {
            topicStack.push(elem);
        });
        //if (!result.topic) {
        //    var currentTopic = topicStack.pop();
        //    result.topic = currentTopic;
        //}
        result.topicStack = topicStack;

    }

}


